package com.rfid.base.base;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseListActivity <T extends BaseAdapter,Y,G extends BasePresenter> extends BaseActivity<G> implements Listener<Y>  {

    protected RecyclerView mRecyclerView;
    protected T mAdapter;
    protected List<Y> mData = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResId());
        mAdapter = getAdapter();
        if (mAdapter != null) {
            mAdapter.setListener(this);
            mRecyclerView = findViewById(getRecyclerViewId());
            mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
            mRecyclerView.setAdapter(mAdapter);
        }
    }

    @Override
    public void onClick(Y y) {
    }

    @Override
    public void onLongClick(Y y) {
    }

    @Override
    public void onSelect(Y y, boolean select) {
    }

    public void setData(List<Y> books) {
        System.out.println("setData");
        if (books != mData) {
            mData.clear();
            mData.addAll(books);
        }
        if (mAdapter != null) {
            mAdapter.notifyDataSetChanged();
        }
    }

    public void addData(List<Y> data) {
        mData.addAll(data);
        if (mAdapter != null) {
            mAdapter.notifyDataSetChanged();
        }
    }

    protected abstract int getLayoutResId();

    protected abstract T getAdapter();

    protected abstract int getRecyclerViewId();

}
